package com.huifaapp.mall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.huifaapp.mall.R;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.selectImage.SelectImageView;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

public class ComplaintActivity extends AppCompatActivity {

    private EditText ed_content;
    private RadioButton rb1,rb2,rb3,rb4;
    private EditText ed_name,ed_phone;
    private int reason=0;
    private Button bt_commit;
    SelectImageView selectImageView;
    private ImageView fanhui;
    private Button bt_commit_no;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complaint);
        initView();
        initData();
    }


    private void initView() {
        fanhui = (ImageView) findViewById(R.id.tousu_back);
        ed_content = (EditText) findViewById(R.id.complaint_ed);
        ed_name = (EditText) findViewById(R.id.complaint_name);
        ed_phone= (EditText) findViewById(R.id.complaint_phone);
        rb1 = (RadioButton) findViewById(R.id.rb1);
        rb2 = (RadioButton) findViewById(R.id.rb2);
        rb3 = (RadioButton) findViewById(R.id.rb3);
        rb4 = (RadioButton) findViewById(R.id.rb4);
        bt_commit = (Button) findViewById(R.id.complaint_commit_true);
        bt_commit_no = (Button) findViewById(R.id.complaint_commit);

        ed_phone.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听
                Log.e("输入过程中执行该方法", "文字变化");
                if(ed_phone.getText().length()==11){
                    bt_commit.setVisibility(View.VISIBLE);
                    bt_commit_no.setVisibility(View.GONE);
                }else{
                    bt_commit.setVisibility(View.GONE);
                    bt_commit_no.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // 输入前的监听
                Log.e("输入前确认执行该方法", "开始输入");

            }

            @Override
            public void afterTextChanged(Editable s) {
                // 输入后的监听
                Log.e("输入结束执行该方法", "输入结束");

            }
        });

        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 finish();
            }
        });
        selectImageView = new SelectImageView();
        selectImageView.setMaxNum(3);
        getSupportFragmentManager().beginTransaction().add(R.id.fragmentContent, selectImageView).commit();
    }
    private void initData() {

        rb1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reason = 001;
            }
        });
        rb2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reason = 002;
            }
        });
        rb3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reason = 003;
            }
        });
        rb4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reason = 004;
            }
        });



            bt_commit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    try {
                        final String content= ed_content.getText().toString();
                        final String phonenum = ed_phone.getText().toString();
                        final String name = ed_name.getText().toString();
                        if (content.equals("")||phonenum.equals("")||name.equals("")){
                            ToastUtils.showShort("请填写完整");
                        }else {
                            try {
                                OkHttpUtils.post().url(ApiManager.BASE_NIUPAI_URL+"users/complaint/submit")
                                        .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                                        .addParams("appId", AUniqueIdentifier.getDeviceId(ComplaintActivity.this))
                                        .addParams("content",content)
                                        .addParams("reason",reason+"")
                                        .addParams("phone",phonenum)
                                        .addParams("name",name)
                                        .build()
                                        .execute(new StringCallback() {
                                            @Override
                                            public void onError(Request request, Exception e) {}
                                            @Override
                                            public void onResponse(String response) {
                                                Log.e("投诉建议url-----",response);
                                                ToastUtils.showShort("提交成功");
                                                finish();
                                            }
                                        });
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int arg1, Intent arg2) {
        super.onActivityResult(requestCode, arg1, arg2);
        selectImageView.onActivityResult(requestCode, arg1, arg2);
    }
}
